﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using Telerik.WebControls;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Upload;
using System.Collections.Generic;

public partial class sysadmin_NewManager_UploadFile : PowerCreator.LMS.Web.Base.BasePage
{
    Course opCourse = new Course();
    string id = "0";
    string Url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
        }
    }

    protected override void InitPage()
    {

        lbl_UploadFileInfoTitle.Text = "上传文件";
        lbl_File.Text = GetLanageValueByKey("lbl_UploadResource_File", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_UploadFile.Text = GetLanageValueByKey("btn_UploadResource_UploadFile", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Back.Text = GetLanageValueByKey("btn_UploadResource_Back", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_Description_title.Text = GetLanageValueByKey("lbl_UploadResource_Description_title", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_1.Text = GetLanageValueByKey("lbl_UploadResource_Description_1", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_2.Text = GetLanageValueByKey("lbl_UploadResource_Description_2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_3.Text = GetLanageValueByKey("lbl_UploadResource_Description_3", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Description_4.Text = GetLanageValueByKey("lbl_UploadResource_Description_4", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "html";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = "只允许上传文件类型为：" + allowExtensions;
            return false;
        }
        return true;
    }
    protected void btnUploadFile_Click(object sender, EventArgs e)
    {
        btn_UploadFile.Enabled = false;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/NewTempletFile/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);
            string surl = "";
            if (Request.QueryString["ID"] != null)
            {
                id = Request.QueryString["ID"].ToString();
            }
            if (id == "0")
            {
                surl = "AddNewTemplet.aspx?Type=upload&fullpath=" + Url;
            }
            else
            {
                surl = "AddNewTemplet.aspx?Type=upload&ID=" + id + "&fullpath=" + Url;
            }
            Script.RedirectURL(surl);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string surl = "";
        if (Request.QueryString["ID"] != null)
        {
            id = Request.QueryString["ID"].ToString();
        }
        if (id == "0")
        {
            surl = "AddNewTemplet.aspx";
        }
        else
        {
            surl = "AddNewTemplet.aspx?ID=" + id;
        }
        Script.RedirectURL(surl);
    }
}
